//
//  CollectViewController.m
//  MagicZhao
//
//  Created by hy002 on 2017/12/21.
//  Copyright © 2017年 HUAYU. All rights reserved.
//

#import "CollectViewController.h"
#import "CollectionModel.h"
#import "FactoryDetailsViewController.h"
static NSString *cellPath = @"Collectcell";
@interface CollectViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableview;
@property (nonatomic, assign)NSInteger page;
@property (nonatomic, strong)NSMutableArray *collectAry;

@end

@implementation CollectViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    _page = 0;
    [self requestMyCollect];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 0;
    _collectAry = [NSMutableArray arrayWithCapacity:0];
    [self creatUI];
}

- (void)creatUI
{
    self.navigationItem.title = @"我的收藏";
    self.view.backgroundColor = BacksColor;
    self.navigationController.navigationBar.translucent = NO;
    
    CGFloat height = SCREEN_HEIGHT-STA_NAV_HEIGHT-SCREER_HeiX;
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, height) style:UITableViewStylePlain];
    _tableview.backgroundColor = BacksColor;
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.tag = 20;
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableview];
    
    self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewHeaderData)];
    self.tableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreFootData)];
    self.tableview.mj_footer.automaticallyHidden = YES;
    self.tableview.estimatedRowHeight = 0;
    self.tableview.estimatedSectionHeaderHeight = 0;
    self.tableview.estimatedSectionFooterHeight = 0;
    
}

- (void)loadNewHeaderData
{
    _page = 0;
    [self requestMyCollect];
    
}

- (void)loadMoreFootData
{
    _page ++;
    [self requestMyCollect];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return _collectAry.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellPath];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
        cell.backgroundColor = BacksColor;
        UIView *whiteview = [[UIView alloc] initWithFrame:CGRectMake(5*displaywidth, 5*displaywidth, SCREEN_WIDTH-10*displaywidth, 125*displaywidth)];
        whiteview.backgroundColor = [UIColor whiteColor];
        whiteview.layer.cornerRadius = 5;
        whiteview.layer.masksToBounds = YES;
        [cell.contentView addSubview:whiteview];
        if (_collectAry.count > 0)
        {
            CollectionModel *model = _collectAry[indexPath.row];
            UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(5*displaywidth, 5*displaywidth, SCREEN_WIDTH-10*displaywidth, 125*displaywidth)];
            whiteView.layer.cornerRadius = 10*displaywidth;
            whiteView.layer.masksToBounds = YES;
            [cell.contentView addSubview:whiteView];
            UIImageView *imgView = [ZCControl createImageViewWithFrame:CGRectMake(5*displaywidth, 12.0*displaywidth, 100*displaywidth, 90*displaywidth) ImageName:@""];
            [imgView sd_setImageWithURL:URL(model.headImg)];
            imgView.contentMode = UIViewContentModeScaleAspectFill;
            imgView.layer.masksToBounds = YES;
            [whiteView addSubview:imgView];
            
            UILabel *nameLab = [ZCControl createLabelWithFrame:CGRectMake(115*displaywidth, 10*displaywidth, SCREEN_WIDTH-160*displaywidth, 25*displaywidth) Font:16.0 Text:model.name Color:RGB(129, 148, 196)];
            [whiteview addSubview:nameLab];
            UIButton *deleteBtn = [ZCControl createButtonWithFrame:CGRectMake(CGRectGetMaxX(nameLab.frame), 10*displaywidth, 30*displaywidth, 30*displaywidth) ImageName:@"lajit" Target:self Action:@selector(onClickDeleteBtn:) Title:nil Font:10];
            deleteBtn.tag = indexPath.row;
            [whiteView addSubview:deleteBtn];
            UILabel *addressLab = [ZCControl createLabelWithFrame:CGRectMake(115*displaywidth, 35*displaywidth, SCREEN_WIDTH-135*displaywidth, 50*displaywidth) Font:13.0 Text:model.address Color:RGB(77, 77, 77)];
            [whiteview addSubview:addressLab];
            
            NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"完成单数：%@单",model.finishCount]];
            NSRange titleRange = [[title string]rangeOfString:[NSString stringWithFormat:@"%@",model.finishCount]];
            [title addAttribute:NSForegroundColorAttributeName value:RGB(236, 114, 106) range:titleRange];
            [title addAttribute:NSFontAttributeName value:NormalFontWithSize(12.0+fontScale) range:titleRange];
            UILabel *finishLab = [ZCControl createLabelWithFrame:CGRectMake(115*displaywidth, 85*displaywidth, SCREEN_WIDTH-80*displaywidth, 25*displaywidth) Font:12.0 Text:nil Color:RGB(128, 128, 128)];
            finishLab.attributedText = title;
            [whiteview addSubview:finishLab];
            
            NSMutableAttributedString *title1 = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"评价分数：%@ 好评",model.grade]];
            NSRange titleRange1 = [[title1 string]rangeOfString:[NSString stringWithFormat:@"%@",model.grade]];
            [title1 addAttribute:NSForegroundColorAttributeName value:RGB(229, 166, 96) range:titleRange1];
            [title1 addAttribute:NSFontAttributeName value:NormalFontWithSize(12.0+fontScale) range:titleRange1];
            UILabel *gradeLab = [ZCControl createLabelWithFrame:CGRectMake(SCREEN_WIDTH-150*displaywidth, 85*displaywidth, 130*displaywidth, 25*displaywidth) Font:12.0 Text:nil Color:RGB(128, 128, 128)];
            gradeLab.textAlignment = NSTextAlignmentRight;
            gradeLab.attributedText = title1;
            [whiteview addSubview:gradeLab];
        }
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionModel *model = _collectAry[indexPath.row];
    FactoryDetailsViewController *vc = [[FactoryDetailsViewController alloc] init];
    vc.companyID = model.ID;
    [self.navigationController pushViewController:vc animated:YES];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 130*displaywidth;
}

- (void)onClickDeleteBtn:(UIButton *)sender {
    CollectionModel *model = _collectAry[sender.tag];
    WEAKSELF;
    //    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HYBaseHandler requestCollectAddOrRemoveParmers:@{@"contractorId":[NSString stringWithFormat:@"%@",model.ID]} Success:^(id obj)
     {
         //         [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
         if ([obj server_Getcodecheck])
         {
             //             [LTools showMBProgressWithText:@"删除成功" addToView:weakSelf.view];
             [self requestMyCollect];
         }
         else
         {
             [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
         }
     } failed:^(NSError *error) {
         //         [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
         
     }];
}

- (void)requestMyCollect
{
    WEAKSELF;
    NSDictionary *dic = @{@"page":[NSString stringWithFormat:@"%ld",_page]};
    [HYBaseHandler requestMyCollectParmers:dic Success:^(id obj) {
        [weakSelf.tableview.mj_header endRefreshing];
        [weakSelf.tableview.mj_footer endRefreshing];
        if ([obj server_Getcodecheck])
        {
            NSArray *orderArray = [CollectionModel mj_objectArrayWithKeyValuesArray:obj[@"data"][@"content"]];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                if (weakSelf.page == 0)
                {
                    [weakSelf.collectAry removeAllObjects];
                }
                [weakSelf.collectAry addObjectsFromArray:orderArray];
                [weakSelf.tableview reloadData];
            });
            
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        [weakSelf.tableview.mj_header endRefreshing];
        [weakSelf.tableview.mj_footer endRefreshing];
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
